<template>
  <!-- 统计功能区域（可点击跳转） -->
  <el-row class="stats-grid" justify="space-around">
    <el-col :span="6" class="stat-item" @click="goToPage('collection')">
      <span class="value">{{ collect }}</span>
      <span class="label">我的收藏</span>
    </el-col>
    <el-col :span="6" class="stat-item" @click="goToPage('history')">
      <span class="value">{{ history }}</span>
      <span class="label">历史浏览</span>
    </el-col>
    <el-col :span="6" class="stat-item" @click="goToPage('follow')">
      <span class="value">{{ follow }}</span>
      <span class="label">我的关注</span>
    </el-col>
    <el-col :span="6" class="stat-item" @click="goToPage('coupon')">
      <span class="value">{{ coupon }}</span>
      <span class="label">红包卡券</span>
    </el-col>
  </el-row>
  <el-loading :visible="loading"></el-loading>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios'; // 用于接口请求

// 路由实例
const router = useRouter();

// 统计数据
const collect = ref(0);
const history = ref(0);
const follow = ref(0);
const coupon = ref(0);
const loading = ref(true);

// 跳转详情页方法
const goToPage = (type: string) => {
  // 根据类型跳转不同页面，并传递对应数量
  const params = {
    collection: { path: '/personal/collection', count: collect.value },
    history: { path: '/personal/history', count: history.value },
    follow: { path: '/personal/follow', count: follow.value },
    coupon: { path: '/personal/coupon', count: coupon.value },
  };
  const target = params[type as keyof typeof params];
  router.push({
    path: target.path,
    query: { count: target.count }, // 通过query传递数量
  });
};

// 模拟从接口获取数据
const fetchStatsData = async () => {
  try {
    const res = await axios.get('http://localhost:8080/api/user/stats');
    const data = res.data;
    collect.value = data.collect || 0;
    history.value = data.history || 0;
    follow.value = data.follow || 0;
    coupon.value = data.coupon || 0;
  } catch (err) {
    console.error('获取统计数据失败:', err);
  } finally {
    loading.value = false;
  }
};

// 组件挂载时加载数据
onMounted(() => {
  fetchStatsData();
});
</script>

<style scoped>
.user-panel {
  padding: 20px;
  background: #f8f8f8;
  border-radius: 12px;
}

.user-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 15px;
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
}

.info-text {
  flex: 1;
  margin-left: 15px;
}

.nickname {
  font-size: 18px;
  font-weight: 500;
  color: #333;
}

.fish-value {
  font-size: 14px;
  color: #666;
  margin-top: 5px;
}

.action-icons {
  display: flex;
  gap: 15px;
}

.icon {
  font-size: 22px;
  color: #666;
  cursor: pointer;
  transition: color 0.3s;
}

.icon:hover {
  color: #409eff;
}

.stats-grid {
  background: #fff;
  padding: 15px;
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.stat-item {
  text-align: center;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  border-radius: 8px;
  transition: background-color 0.2s;
}

.stat-item:hover {
  background-color: #f5f5f5;
}

.value {
  font-size: 20px;
  font-weight: 500;
  color: #333;
}

.label {
  font-size: 14px;
  color: #666;
  line-height: 1.2;
}
</style>